//
//  NSString+formatWithFloat.m
//  YaoHeAPP
//
//  Created by xyh on 16/5/17.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NSString+formatWithFloat.h"

@implementation NSString (formatWithFloat)

//格式话小数 四舍五入类型
+ (NSString *)decimalwithFormat:(NSString *)format floatV:(float)floatV {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    
    [numberFormatter setPositiveFormat:format];
    
    return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]];
}

//*
//*    @param     price     需要转化的数据
//*    @param     position     有效小数位
//*
//*    @return    截取后数据
//*    如果不想四舍五入直接截取的话就把 NSRoundPlain改为NSRoundDown即可
//*/
+ (NSString *)notRounding:(NSDecimalNumber *)price afterPoint:(NSInteger)position {
    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSString *tempString=[NSString stringWithFormat:@"%@", [price decimalNumberByRoundingAccordingToBehavior:roundingBehavior]];
    
    NSString *OutZeroString=[NSString cutOutZeroToEndWithString:tempString];
    
    return OutZeroString;
}

+ (NSString *)cutOutZeroToEndWithString:(NSString *)tempString {
    if ([tempString containsString:@"."]) {
        for (int i = (int)tempString.length; i >= 0; i--) {
            NSString *lastChar = [tempString substringWithRange:NSMakeRange(i-1, 1)];
            if ([lastChar isEqualToString:@"0"]) {
                tempString = [tempString substringToIndex:i-1];
                continue;
            } else {
                NSUInteger len = tempString.length;
                if ([[tempString substringFromIndex:len-1] isEqualToString:@"."]) {
                    tempString = [tempString substringToIndex:len-1];
                }
                return tempString;
            }
        }
    } else {
        return tempString;
    }
    return nil;
}

@end
